/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.ejb;

import com.mycompany.psc.controllers.Encryptor;
import com.mycompany.psc.data.Credentials;
import com.mycompany.psc.data.Roles;
import com.mycompany.psc.entities.Department;
import com.mycompany.psc.entities.Projects;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.Auth;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author A7med
 */
@Stateless
public class UsersManager {

    @PersistenceContext(unitName = "com.mycompany_PSC_war_1.0-SNAPSHOTPU")
    EntityManager em;
    @Inject
    @Auth
    Encryptor auth;
    @Inject
    Credentials credentials;

    public int SaveUser(String name, String password, Department role) {

        try {
            Users u = new Users();
            u.setName(name);
            u.setPassword(auth.encrypt(password));
            u.setDepartment(role);
            em.persist(u);
            return 0;
        } catch (Exception e) {
            return -1;
        }

    }

    private List<Users> getNamedQueryResult(String namedQuery, Map<String, Object> params) {
        TypedQuery<Users> data = em.createNamedQuery(namedQuery, Users.class);
        for (String key : params.keySet()) {
            data.setParameter(key, params.get(key));
        }

        List<Users> list = data.getResultList();
        return list;

    }

    public boolean IsUserExists(String name) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("name", name);
        List<Users> list = getNamedQueryResult("Users.findByName", params);
        return !list.isEmpty();
    }

    public Users validateUser() {
        HashMap<String, Object> params = new HashMap<>();
         params.put("password", auth.encrypt(credentials.getPassword()));
        params.put("name", credentials.getUsername());
       
        List<Users> list = getNamedQueryResult("Users.login", params);
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }
    public List<Users>getAllUsers(){
        TypedQuery<Users> query = em.createNamedQuery("Users.findAll", Users.class);
        
        return query.getResultList();
    }
     public Users getUserByName(String name){
        TypedQuery<Users> query = em.createNamedQuery("Users.findByName", Users.class);
        query.setParameter("name", name);
        return query.getResultList().get(0);
    }
     public void updataeUsers(Users[]list,Projects p){
         for(Users u:list){
             u.getProjectsList().add(p);
             em.merge(u);
         }
     }

    public List<Projects> getUserProject(Users current) {
        Users u=em.find(Users.class, current.getId());
        return u.getProjectsList();
    }
}
